package pro.spring.controller.backController;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pro.spring.model.Udiscusser;
import pro.spring.vo.PaginationVo;

@Controller
@RequestMapping("diser")
public class DiscusserController extends BaseController{
	
	private String local = "page/back/discuss/";
	
	private String defaultpath="redirect:list.html";
	
	private Integer pagesize = 10 ;
	
	private PaginationVo<Udiscusser> pagelist ;
	
	@RequestMapping(value="list.html",method=RequestMethod.GET)
	public String discussList(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,String key){
		pagelist = udiscusserService.getPaginationInfo(key,pagenum, pagesize);
		map.put("page", pagelist);
		map.put("key",key);
		return local+"discusserList";
	}
	
	

	@RequestMapping(value="lis.html",method=RequestMethod.POST)
	public String discussLists(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,String key){
		pagelist = udiscusserService.getPaginationInfo(key,pagenum, pagesize);
		map.put("page", pagelist);
		map.put("key",key);
		return local+"discusserList";
	}
	
	@RequestMapping(value="{id}.html",method=RequestMethod.GET)
	public String delone(@PathVariable int id){
		udiscusserService.deleteByPrimaryKey(id);
		return defaultpath;
	}
	
	@RequestMapping(value="see/{id}.html",method=RequestMethod.GET)
	public String seeOne(@PathVariable int id,@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,Integer a){
		Udiscusser dis = udiscusserService.selectByPrimaryOn(id,pagenum,pagesize);
		dis.setUsers(usersService.selectByPrimaryKey(dis.getUsersUId()));
		Integer page =0 ;
		Integer count = udiscussService.selectCountBydrId(id);
		System.out.println(count);
		if(count==0){
			page = 1;
		}else if(count%pagesize==0){
			page = count/pagesize;
		}else{
			page = count/pagesize+1;
		}
		map.put("drId",id);
		map.put("pageCount",page);
		map.put("json",dis);
		map.put("pageNum",pagenum);
		if(a==null){
			return local+"discussList";
		}else{
			return local+"theme";
		}
	}
	
	@RequestMapping(value="xian.html",method=RequestMethod.GET)
	public String xianxia(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map){
		pagelist = udiscusserService.getByXianxia(3, pagesize, pagenum);
		map.put("leng",(Integer)pagelist.getList().size());
		map.put("key",3);
		map.put("pages",pagelist);
		return local+"xianxia";
	}
	
	@RequestMapping(value="tong.html",method=RequestMethod.GET)
	public String tong(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map){
		pagelist = udiscusserService.getByXianxia(5, pagesize, pagenum);
		map.put("leng",(Integer)pagelist.getList().size());
		map.put("key",5);
		map.put("pages",pagelist);
		return local+"xianxia";
	}
	
	@RequestMapping(value="wei.html",method=RequestMethod.GET)
	public String wei(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map){
		pagelist = udiscusserService.getByXianxia(4, pagesize, pagenum);
		map.put("key",4);
		map.put("leng",(Integer)pagelist.getList().size());
		map.put("pages",pagelist);
		return local+"xianxia";
	}
	
	@RequestMapping(value="fabu/{id}.html",method=RequestMethod.GET)
	public String fabu(@PathVariable int id){
		Udiscusser u = udiscusserService.selectByPrimaryKey(id);
		u.setDrType(5);//通过
		udiscusserService.updateByPrimaryKey(u);
		return "redirect:../wei.html";
	}
	
	@RequestMapping(value="fbNo/{id}.html",method=RequestMethod.GET)
	public String fbNo(@PathVariable int id){
		Udiscusser u = udiscusserService.selectByPrimaryKey(id);
		u.setDrType(4);//未通过
		udiscusserService.updateByPrimaryKey(u);
		return "redirect:../tong.html";
	}
	
	
}